package vashion.azeroth.core.manager.item;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.dao.BaseDao;
import vashion.azeroth.core.item.dao.ItemYvogueBrandDao;
import vashion.azeroth.core.item.pojo.ItemYvogueBrandDO;
import vashion.azeroth.core.manager.impl.BaseManagerImpl;
import vashion.azeroth.core.result.ApiResult;

/**
 * Created by Loki on 17/4/26. yvogue品牌管理
 */
@Service
public class ItemYvogueBrandManager extends BaseManagerImpl<ItemYvogueBrandDO> {

	private final static Logger log = LoggerFactory.getLogger(ItemYvogueBrandManager.class);

	@Autowired
	private ItemYvogueBrandDao itemYvogueBrandDao;

	@Override
	public BaseDao<ItemYvogueBrandDO> getDao() {
		return itemYvogueBrandDao;
	}

	/**
	 * 按照yvogue品牌名精确查找品牌类
	 * 
	 * @param yvogueBrandTitle
	 * @return yvogue品牌名为yvogueBrandtitle的品牌类
	 */
	public ApiResult<ItemYvogueBrandDO> findByYvogueBrandTitle(String yvogueBrandTitle) {
		try {
			ItemYvogueBrandDO itemYvogueBrandDO = itemYvogueBrandDao.findByYvogueBrandTitle(yvogueBrandTitle);
			return ApiResult.newSuccessResult(itemYvogueBrandDO);
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	/**
	 * 保存yvogue品牌和本系统的品牌的关联
	 * 
	 * @param itemYvogueBrandDO
	 * @return yvogue品牌和系统品牌关联保存结果
	 */
	public ApiResult<Boolean> publishYvogueBrand(ItemYvogueBrandDO itemYvogueBrandDO) {
		ApiResult<ItemYvogueBrandDO> yvoguoBrandResult = this
				.findByYvogueBrandTitle(itemYvogueBrandDO.getYvogueBrandTitle());
		try {
			if (yvoguoBrandResult.isSuccess()) {
				if (null == yvoguoBrandResult.getData()) {
					itemYvogueBrandDao.save(itemYvogueBrandDO);
					return ApiResult.newSuccessResult(true);
				} else {
					return ApiResult.newSuccessResult(false);
				}
			} else {
				return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
						GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
			}
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

}
